
/**
 * [uploadFile] - 上传切片参数
 * @param data
 * @param onCancel
 */

// 上传单个切片
export async function uploadFile(data, onCancel) {
    const controller = new AbortController()
    const signal = controller.signal // 获取 signal 对象
    const response = await fetch('/prod-api/upload', {
        method: "POST", // *GET, POST, PUT, DELETE, etc.
        mode: "cors", // no-cors, *cors, same-origin
        cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
        credentials: "same-origin", // include, *same-origin, omit
        /*headers: {
            "Content-Type": "multipart/form-data",
            // 'Content-Type': 'application/x-www-form-urlencoded',
        },*/
        redirect: "follow", // manual, *follow, error
        referrerPolicy: "no-referrer", // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
        body: data, // body data type must match "Content-Type" header
        signal
    });
    // 如果提供了 onCancel 回调，则传递取消函数
    if (typeof onCancel === 'function') {
        // 如果是一个函数，则直接调用传一个取消方法给 这个方法
        // 所以只要传进来是方法，就会直接传一个参数并直接触发这个函数
        // 那传过来的这个方法就会接收到一个参数（就是取消函数() => controller.abort()）
        // 在调用uploadFile就可以拿到这个参数
        onCancel(() => controller.abort()) // 调用 onCancel 时传入取消函数
    }
    return response.blob()
}

/**
 * [mergeChunk] - 合并切片
 * @param data
 */

// 合并所有切片
export async function mergeChunk(data) {
    const response = await fetch('/prod-api/merge', {
        method: "POST", // *GET, POST, PUT, DELETE, etc.
        mode: "cors", // no-cors, *cors, same-origin
        cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
        credentials: "same-origin", // include, *same-origin, omit
        headers: {
            "Content-Type": "application/json",
            // 'Content-Type': 'application/x-www-form-urlencoded',
        },
        redirect: "follow", // manual, *follow, error
        referrerPolicy: "no-referrer", // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
        body: JSON.stringify(data), // body data type must match "Content-Type" header
    });
    return response.json();
}

/**
 * [checkFile] - 检查文件是否存在
 * @param data
 */

// 检查文件是否存在
export async function checkFile(data) {
    const response = await fetch('/prod-api/verify', {
        method: "POST", // *GET, POST, PUT, DELETE, etc.
        mode: "cors", // no-cors, *cors, same-origin
        cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
        credentials: "same-origin", // include, *same-origin, omit
        headers: {
            "Content-Type": "application/json",
            // 'Content-Type': 'application/x-www-form-urlencoded',
        },
        redirect: "follow", // manual, *follow, error
        referrerPolicy: "no-referrer", // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
        body: JSON.stringify(data), // body data type must match "Content-Type" header
    });
    return response.json();
}
// 检查文件是否存在
export async function getFiles(data) {
    const response = await fetch('/prod-api/getFiles', {
        method: "POST", // *GET, POST, PUT, DELETE, etc.
        mode: "cors", // no-cors, *cors, same-origin
        cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
        credentials: "same-origin", // include, *same-origin, omit
        headers: {
            "Content-Type": "application/json",
            // 'Content-Type': 'application/x-www-form-urlencoded',
        },
        redirect: "follow", // manual, *follow, error
        referrerPolicy: "no-referrer", // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
        body: JSON.stringify(data), // body data type must match "Content-Type" header
    });
    return response.json();
}

export async function download({fileName, start, end}) {
    return await fetch('http://localhost:9531/prod-api/download', {
        headers: {
            'range': `bytes=${start}-${end}`
        },
        method: 'POST',
        body: JSON.stringify({
            filename: fileName
        })
    });
}
